<?php

namespace backend\controllers;

use Yii;
use common\models\projectrepo\ProjectRepo;
use common\models\projectrepo\ProjectRepoSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ProjectRepoController implements the CRUD actions for ProjectRepo model.
 */
class ProjectRepoController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all ProjectRepo models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ProjectRepoSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single ProjectRepo model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        
        $model->schedule_target = json_decode($model->schedule_target);
        $model->schedule_starttime = $this->dateFormat(json_decode($model->schedule_starttime));
        $model->schedule_endtime = $this->dateFormat(json_decode($model->schedule_endtime));
        $model->mainpart_name = json_decode($model->mainpart_name);
        $model->mainpart_age = json_decode($model->mainpart_age);
        $model->mainpart_profession = json_decode($model->mainpart_profession);
        $model->mainpart_unit = json_decode($model->mainpart_unit);
        $model->mainpart_mainwork = json_decode($model->mainpart_mainwork);
        $model->mainpart_note = json_decode($model->mainpart_note);
        $model->paid_content = json_decode($model->paid_content);
        $model->paid_money = json_decode($model->paid_money);
        $model->paid_note = json_decode($model->paid_note);
        
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new ProjectRepo model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ProjectRepo();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing ProjectRepo model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            // print_r(Yii::$app->request->post());die;
            $model->application_date = strtotime($model->application_date);
            $model->schedule_target = json_encode($model->schedule_target);
            $model->schedule_starttime = json_encode($this->strtoFormat($model->schedule_starttime));
            $model->schedule_endtime = json_encode($this->strtoFormat($model->schedule_endtime));
            $model->mainpart_name = json_encode($model->mainpart_name);
            $model->mainpart_age = json_encode($model->mainpart_age);
            $model->mainpart_profession = json_encode($model->mainpart_profession);
            $model->mainpart_unit = json_encode($model->mainpart_unit);
            $model->mainpart_mainwork = json_encode($model->mainpart_mainwork);
            $model->mainpart_note = json_encode($model->mainpart_note);
            $model->paid_content = json_encode($model->paid_content);
            $model->paid_money = json_encode($model->paid_money);
            $model->paid_note = json_encode($model->paid_note);
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
            
        } else {

            $model->application_date = date('Y-m-d H:i', $model->application_date);
            $model->schedule_target = json_decode($model->schedule_target);
            $model->schedule_starttime = $this->dateFormat(json_decode($model->schedule_starttime));
            $model->schedule_endtime = $this->dateFormat(json_decode($model->schedule_endtime));
            $model->mainpart_name = json_decode($model->mainpart_name);
            $model->mainpart_age = json_decode($model->mainpart_age);
            $model->mainpart_profession = json_decode($model->mainpart_profession);
            $model->mainpart_unit = json_decode($model->mainpart_unit);
            $model->mainpart_mainwork = json_decode($model->mainpart_mainwork);
            $model->mainpart_note = json_decode($model->mainpart_note);
            $model->paid_content = json_decode($model->paid_content);
            $model->paid_money = json_decode($model->paid_money);
            $model->paid_note = json_decode($model->paid_note);

            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing ProjectRepo model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the ProjectRepo model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ProjectRepo the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ProjectRepo::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    protected function strtoFormat($arr)
    {

        foreach ($arr as $t) {
            $res[] = strtotime($t);
        }
        return $res;
    
    }
    protected function dateFormat($str)
    {
        //if 解决新增项目之后，首次修改申报书时报错问题
        if (!empty($str)) {
            foreach ($str as $t) {
                $res[] = date('Y-m-d H:i', $t);
            }
            return $res;
        }
        return array();
    }
}
